package com.chang.bootplus.controller;


import com.chang.bootplus.base.annotation.PublicVisit;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.controller.storage.local.LocalService;
import com.chang.bootplus.po.*;
import com.chang.bootplus.service.NewsService;
import com.chang.bootplus.vo.NewsVO;
import com.sun.org.apache.xpath.internal.operations.Mult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-01-23
 */
@Api(tags = {"新闻列表"})
@RestController
@RequestMapping("/news")
@CrossOrigin(origins = "*")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NewsController {

    final NewsService newsService;
    final LocalService localService;

    @PublicVisit
    @PostMapping("/listNews")
    @ApiOperation(httpMethod = "POST", value = "前台展示特定的资讯")
    public ApiResponse<NewsVO> listNews(@RequestBody NewsSelPO po){
        return newsService.listNews(po);
    }

    @PostMapping(value = "/addNews",headers = "content-type=multipart/form-data")
    @ApiOperation(httpMethod = "POST", value = "添加资讯")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",
            dataTypeClass = MultipartFile.class,
            required = true,
            allowMultiple = true)
    })
    public ApiResponse<Void> addNews(@RequestParam("file") MultipartFile file,@RequestParam("title") String title ,
                                     @RequestParam("label") String label,@RequestParam("field") String field,
                                     @RequestParam("context") String context,@RequestParam("score") Integer score){

        String url = (String) localService.uploadFile(file, "dodo").getBody().get("fileUrl");

        return newsService.addNews(new NewsPO().setContext(context).setField(field).setLabel(label).setTitle(title).setImage(url).setScore(score));
    }

//    @PostMapping(value = "/updateNews",headers = "content-type=multipart/form-data")
//    @ApiOperation(httpMethod = "POST", value = "修改资讯")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "file",
//                    dataTypeClass = MultipartFile.class,
//                    required = true,
//                    allowMultiple = true)
//    })
//    public ApiResponse<Void> updateNews(@RequestParam(value = "file",required = false) MultipartFile file,
//                                        @RequestParam(value = "id",required = true) Long id,
//                                        @RequestParam(value = "title",required = false) String title ,
//                                        @RequestParam(value = "label",required = false) String label,
//                                        @RequestParam(value = "field",required = false) String field,
//                                        @RequestParam(value = "context",required = false) String context,
//                                        @RequestParam(value = "score",required = false) Integer score){
//
//        if(file == null){
//            return newsService.updateNews(new NewsUpdatePO().setContext(context).setField(field).setLabel(label).setTitle(title).setId(id).setScore(score));
//        }
//
//        String url = (String) localService.uploadFile(file, "dodo").getBody().get("fileUrl");
//        return newsService.updateNews(new NewsUpdatePO().setContext(context).setField(field).setLabel(label).setTitle(title).setImage(url).setId(id).setScore(score));
//    }

    @PostMapping("/updateNews")
    @ApiOperation(httpMethod = "POST", value = "修改资讯")
    public ApiResponse<Void> updateNews(@RequestBody NewsUpdatePO po){
        return newsService.updateNews(po);
    }

    @PostMapping("/deleteNews")
    @ApiOperation(httpMethod = "POST", value = "删改资讯")
    public ApiResponse<Void> deleteNews(@RequestBody NewsIdPO po){
        return newsService.deleteNews(po);
    }

    @PostMapping("/listBackNews")
    @ApiOperation(httpMethod = "POST", value = "后台资讯查询")
    public ApiResponse<NewsVO> listBackNews(@RequestBody NewsSelBackPO po){
        return newsService.listBackNews(po);
    }


}



